public class num733 {
    int[] num1 = new int[]{1,0,0,-1};
    int[] num2 = new int[]{0,1,-1,0};
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int oldColor = image[sr][sc];
        if(oldColor != newColor) {
            dfs(image,sr,sc,oldColor,newColor);
        }
        return image;
    }

    private void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) {
        if(image[sr][sc] == oldColor) {
            image[sr][sc] = newColor;
            for (int i = 0; i < num1.length; i++) {
                int x = sr + num1[i];
                int y = sc + num2[i];
                if(x >= 0 && x < image.length && y >= 0 && y < image[0].length) {
                    dfs(image,x,y,oldColor,newColor);
                }
            }
        }else {
            return;
        }
    }
}
